<template>
    <Dialog v-model="dialogVisible" :title="dialogTitle">
        <el-form ref="formRef" v-loading="formLoading" :model="formData" :rules="formRules" label-width="95px">
            <el-form-item label="轮播图名称" prop="name">
                <el-input v-model="formData.name" placeholder="请输入轮播图名称"/>
            </el-form-item>
            <el-form-item label="轮播顺序" prop="sort">
                <el-input-number v-model="formData.sort" :min="1" clearable/>
            </el-form-item>
            <el-form-item label="轮播图" prop="picture">
                <upload-img v-model="formData.picture"/>
            </el-form-item>
            <el-form-item label="类型" prop="type">
                <el-select v-model="formData.type" placeholder="请选择配置类型">
                    <el-option v-for="dict in getIntDictOptions(DICT_TYPE.YXT_BANNER_TYPE)" :label="dict.label" :value="dict.value"/>
                </el-select>
            </el-form-item>
            <el-form-item v-if="formData.type === 1" label="上传长图" prop="redirect">
                <upload-img v-model="formData.redirect"/>
            </el-form-item>
            <el-form-item v-if="formData.type === 2" label="跳转地址" prop="redirect">
                <el-input v-model="formData.redirect" placeholder="请输入跳转链接地址 https://xxxxxx.com" clearable type="textarea" :rows="3"/>
            </el-form-item>
            <el-form-item label="状态" prop="status">
                <el-radio-group v-model="formData.status">
                    <el-radio v-for="dict in getIntDictOptions(DICT_TYPE.YXT_BANNER_STATUS)" :label="dict.value">
                        {{ dict.label }}
                    </el-radio>
                </el-radio-group>
            </el-form-item>
        </el-form>
        <template #footer>
            <el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="dialogVisible = false">取 消</el-button>
        </template>
    </Dialog>
</template>
<script lang="ts" setup>
import {useBannerApi} from "@/api/yxt/banner";
import {DICT_TYPE, getIntDictOptions} from "@/utils/dict";
import {useMessage} from "@/components/Notice";

defineOptions({name: 'YxtBannerForm'})

const {t} = useI18n()
const bannerApi = useBannerApi();

const dialogVisible = ref(false)
const dialogTitle = ref('')
const formLoading = ref(false)
const formType = ref('')
const formData = ref({
    id: undefined,
    name: '',
    sort: 1,
    picture: '',
    type: 0,
    redirect: '',
    status: 1
})
const formRules = reactive({
    name: [{required: true, message: '轮播图名称不能为空', trigger: ['blur', 'change']}],
    picture: [{required: true, message: '轮播图片不能为空', trigger: ['blur', 'change']}],
    redirect: [{required: true, message: '此选项不能为空', trigger: ['blur', 'change']}],
})
const formRef = ref()

const open = async (type: string, id?: number) => {
    dialogVisible.value = true
    dialogTitle.value = t('action.' + type)
    formType.value = type
    resetForm()
    if (id) {
        formLoading.value = true
        try {
            formData.value = await bannerApi.getObj(id)
        } finally {
            formLoading.value = false
        }
    }
}
defineExpose({open})

const emit = defineEmits(['success'])
const submitForm = () => {
    formRef.value?.validate(async (valid: any) => {
        if (valid) {
            formLoading.value = true
            try {
                const data = formData.value;
                if (formType.value === 'create') {
                    await bannerApi.createObj(data);
                    useMessage(t('common.createSuccess'));
                } else {
                    await bannerApi.updateObj(data);
                    useMessage(t('common.updateSuccess'));
                }
                dialogVisible.value = false
                emit('success')
            } finally {
                formLoading.value = false
            }
        }
    });
}

const resetForm = () => {
    formData.value = {
        id: undefined,
        name: '',
        sort: 1,
        picture: '',
        type: 0,
        redirect: '',
        status: 1
    }
    formRef.value?.resetFields()
}
</script>
